package me.jessyan.armscomponent.app.mvp.ui.activity.user;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RelativeLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import java.util.List;

import javax.inject.Inject;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;
import me.jessyan.armscomponent.app.R;
import me.jessyan.armscomponent.app.di.component.DaggerUserTestKnowledgeLevelComponent;
import me.jessyan.armscomponent.app.mvp.contract.user.UserTestKnowledgeLevelContract;
import me.jessyan.armscomponent.app.mvp.model.entity.Question;
import me.jessyan.armscomponent.app.mvp.presenter.user.UserTestKnowledgeLevelPresenter;
import me.jessyan.armscomponent.app.mvp.ui.adapter.UserTestLevelAdapter;
import me.jessyan.armscomponent.commonres.widget.toast.ToastView;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;
import me.jessyan.armscomponent.commonsdk.utils.Utils;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * <p>
 * Author: Taozebi
 * Date: 08/19/2021 23:20
 * Description: describe of this class
 */

@Route(path = RouterHub.USER_TESTKNOWLEDGELEVELACTIVITY)
public class UserTestKnowledgeLevelActivity extends BaseActivity<UserTestKnowledgeLevelPresenter> implements UserTestKnowledgeLevelContract.View {

    @BindView(R.id.user_rv_test_level)
    RecyclerView user_rv_test_level;
    @BindView(R.id.public_toolbar_back)
    RelativeLayout public_toolbar_back;
    @Inject
    RecyclerView.LayoutManager mLayoutManager;
    @Inject
    RecyclerView.Adapter mAdapter;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerUserTestKnowledgeLevelComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.user_activity_test_knowledge_level; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        loadTitle();
        initRecyclerView();
        user_rv_test_level.setAdapter(mAdapter);
    }

    /**
     * 初始化RecyclerView
     */
    private void initRecyclerView() {
        ArmsUtils.configRecyclerView(user_rv_test_level, mLayoutManager);
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                // 此处写拦截操作
                showNotCompleteDialog();
                return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    public void onBackPressed() {
        showNotCompleteDialog();
        super.onBackPressed();
    }

    private void showNotCompleteDialog(){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("温馨提示")
                .setCancelable(false)
                .setMessage("首次登录，请先完成知识水平测试~")
                .setPositiveButton("继续完成", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                })
                .create().show();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ToastView.getInstance().showToast(this, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void loadTitle() {
        String title = "知识水平测试";
        setTitle(title);
        public_toolbar_back.setVisibility(View.INVISIBLE);
//        Toolbar toolbar = (Toolbar) findViewById(R.id.public_toolbar);
//        setSupportActionBar(toolbar);
////        getSupportActionBar().setHomeButtonEnabled(true);
////        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                showTestDialog();//返回
//            }
//        });
    }


    @Override
    public void onSubmitSuccess() {
        ToastView.getInstance().showToastComplete(this, "恭喜您已完成知识水平测试~");
        finish();
    }

    @OnClick({R.id.user_btn_complete,R.id.public_toolbar_back})
    public void onClicked(View view) {
        switch (view.getId()) {
            case R.id.user_btn_complete:
                if (Utils.isFastClick()) {
                    if (((UserTestLevelAdapter) mAdapter).checkAnswers()) {
                        List<Question> answers = ((UserTestLevelAdapter) mAdapter).getAnswers();
                        mPresenter.putQuestionList(answers);
                    }
                }
                break;
            case R.id.public_toolbar_back:
                showTestDialog();
                break;
        }
    }

    private void showTestDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("温馨提示")
                .setCancelable(false)
                .setMessage("首次登录，请先完成知识水平测试~")
                .setPositiveButton("继续测试", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                })
                .create().show();
    }


    @Override
    public Activity getActivity() {
        return this;
    }
}
